/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

typedef struct ListNode ListNode;

struct ListNode *detectCycle(struct ListNode *head) {
	ListNode *fast = head;
	ListNode *slow = head;
	ListNode *meet = NULL;
	int flag = 0;
	while (fast && fast->next) {
		fast = fast->next->next;
		slow = slow->next;
		if (slow == fast) {
			meet = slow;
			flag = 1;
			break;
		}
	}
	if (flag) {
		ListNode *cur1 = head;
		ListNode *cur2 = meet;
		while (cur1 != cur2) {
			cur1 = cur1->next;
			cur2 = cur2->next;
		}
		return cur1;
	} else {
		return NULL;
	}
}